package com.liuhuan.api;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * @version 1.0
 * @Author liuhuan
 * @Date 2024/6/26 20:30
 * @通用返回结果
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult {
    private Integer code;//返回状态码
    private String message;//响应信息
    private Object data;//返回数据

    /**
     * 成功返回结果(带数据)
     *
     * @param data
     * @return
     */
    public static CommonResult success(String message, Object data) {
        return new CommonResult(ResultCode.SUCCESS.getCode(), message, data);
    }

    /**
     * 失败返回结果
     *
     * @return
     */
    public static CommonResult fail() {
        return new CommonResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage(), null);
    }

    /**
     * 失败返回结果(带消息)
     *
     * @param message
     * @return
     */
    public static CommonResult fail(String message) {
        return new CommonResult(ResultCode.FAILED.getCode(), message, null);
    }

    /**
     * 未登录返回结果
     *
     * @return
     */
    public static CommonResult unauthorized() {
        return new CommonResult(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), null);
    }

    /**
     * 未授权返回结果
     *
     * @return
     */
    public static CommonResult forbidden() {
        return new CommonResult(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), null);
    }

}
